package com.example.tasks

import com.google.gson.GsonBuilder

val languageMap = mapOf(
    "English" to "en",
    "Cs" to "cs",
    "Ct" to "ct",
    "German" to "de",
    "French" to "fr",
    "Indonesian" to "id",
    "Italian" to "it",
    "Japanese" to "jp",
    "Korean" to "kr",
    "Portuguese" to "pt",
    "Russian" to "ru",
    "Spanish" to "es",
    "Hindi" to "hi"
)

//eyecolor_help_text
const val keyPrefix = "instanceAiBackgroundDesc"

fun main() {
    toJson(filePath, keyPrefix)

}

fun toJson(filepath: String, keyPrefix: String) {
    val instance = Excel2Xml()
    val sourceData: ArrayList<Array<String?>>? = instance.readFromExcel(filepath)

    buildConfigJson(sourceData, keyPrefix)
}

fun buildConfigJson(sourceData: ArrayList<Array<String?>>?, keyPrefix: String) {
    if (sourceData == null || sourceData.isEmpty()) return
    val languageResults = ArrayList<Array<String?>>(8)
    val keyIndex = HashMap<Int, String>(sourceData.size)
    sourceData[0].forEachIndexed { index, s ->
        if (s != null) {
            val s1 = languageMap[s]
            if (s1 != null) {
                keyIndex[index] = s1
            }
        }
    }

    for (index in 1 until sourceData.size) {
        val key = sourceData[index][0]?.trim()
        if (key?.startsWith(keyPrefix) == false /*&& keys2BeWrite.binarySearch(key) < 0*/) continue
        languageResults.add(sourceData[index])
    }

    val gson = GsonBuilder().setPrettyPrinting().create()

    languageResults.forEach {
        println("*******************************${it[0]}*******************************")
        val map = HashMap<String, String>(it.size)
        it.forEachIndexed { index, s ->
            val language = keyIndex[index]
            if (language != null && s != null) {
                map[language] = s
            }

        }
        println(gson.toJson(map))
    }

}

fun write2Json(key: String?, keyPrefix: String): Boolean =
    key?.startsWith(keyPrefix) == true /*|| keys2BeWrite.binarySearch(key) >= 0*/
